# Importamos las librerías necesarias
import plotly.graph_objects as go
import pandas as pd
BTC = pd.read_csv('../05 - Estudio de técnicas de visualización de datos (PEC2)/BTC.csv')
BTC
| Date | Close/Last | Volume | Open | High | Low | |
|---|---|---|---|---|---|---|
| 0 | 11/23/2022 | 16695.9 | NaN | 16501.4 | 16785.6 | 16539.9 |
| 1 | 11/22/2022 | 16508.8 | NaN | 15817.8 | 16576.3 | 16162.3 |
| 2 | 11/21/2022 | 15836.8 | NaN | 16147.7 | 15937.1 | 15751.7 |
| 3 | 11/20/2022 | 16711.8 | NaN | 16638.3 | 16749.3 | 16670.4 |
| 4 | 11/18/2022 | 16633.8 | NaN | 16691.2 | 16846.3 | 16553.0 |
| ... | ... | ... | ... | ... | ... | ... |
| 379 | 11/05/2021 | 61078.6 | NaN | 62242.1 | 61289.5 | 60828.8 |
| 380 | 11/04/2021 | 62139.5 | NaN | 62439.2 | 62326.3 | 60856.7 |
| 381 | 11/03/2021 | 62464.0 | NaN | 63211.6 | 63077.6 | 62293.8 |
| 382 | 11/02/2021 | 63041.7 | NaN | 61091.8 | 63508.6 | 62810.7 |
| 383 | 11/01/2021 | 61098.7 | NaN | 59952.3 | 61523.4 | 60651.5 |
384 rows × 6 columns
# Convertimos la columna Date, de tipo string, a tipo "datetime"
BTC["Date"] = pd.to_datetime(BTC["Date"])
# Comprobamos que es de tipo timestamp
print(type(BTC.Date[0]))
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
# Ordenamos las fechas de más antigua a más reciente
BTC = BTC.sort_values(by='Date', ascending=True)
BTC
| Date | Close/Last | Volume | Open | High | Low | |
|---|---|---|---|---|---|---|
| 383 | 2021-11-01 | 61098.7 | NaN | 59952.3 | 61523.4 | 60651.5 |
| 382 | 2021-11-02 | 63041.7 | NaN | 61091.8 | 63508.6 | 62810.7 |
| 381 | 2021-11-03 | 62464.0 | NaN | 63211.6 | 63077.6 | 62293.8 |
| 380 | 2021-11-04 | 62139.5 | NaN | 62439.2 | 62326.3 | 60856.7 |
| 379 | 2021-11-05 | 61078.6 | NaN | 62242.1 | 61289.5 | 60828.8 |
| ... | ... | ... | ... | ... | ... | ... |
| 4 | 2022-11-18 | 16633.8 | NaN | 16691.2 | 16846.3 | 16553.0 |
| 3 | 2022-11-20 | 16711.8 | NaN | 16638.3 | 16749.3 | 16670.4 |
| 2 | 2022-11-21 | 15836.8 | NaN | 16147.7 | 15937.1 | 15751.7 |
| 1 | 2022-11-22 | 16508.8 | NaN | 15817.8 | 16576.3 | 16162.3 |
| 0 | 2022-11-23 | 16695.9 | NaN | 16501.4 | 16785.6 | 16539.9 |
384 rows × 6 columns
fig = go.Figure(data=go.Ohlc(x=BTC['Date'],
open=BTC['Open'],
high=BTC['High'],
low=BTC['Low'],
close=BTC['Close/Last']))
fig.show()
# Información principal
fig = go.Figure(data=go.Ohlc(x=BTC['Date'],
open=BTC['Open'],
high=BTC['High'],
low=BTC['Low'],
close=BTC['Close/Last']))
# Añadir anotaciones adicionales
fig.update_layout(
title='Evolución precio Bitcoin (último año)',
yaxis_title='Precio en dólares ($)',
shapes = [dict(
x0='2021-11-08', x1='2021-11-08', y0=0, y1=1, xref='x', yref='paper',
line_width=2)],
annotations=[dict(
x='2021-11-08', y=0.05, xref='x', yref='paper',
showarrow=False, xanchor='left', text='Bitcoin All Time High (ATH) - 08/11/2021')]
)
# Mostrar gráfico
fig.show()
fig = go.Figure(data=go.Candlestick(x=BTC['Date'],
open=BTC['Open'],
high=BTC['High'],
low=BTC['Low'],
close=BTC['Close/Last']))
fig.show()
ETH = pd.read_csv('../05 - Estudio de técnicas de visualización de datos (PEC2)/ETH.csv')
ETH
| Date | Close/Last | Volume | Open | High | Low | |
|---|---|---|---|---|---|---|
| 0 | 11/26/2022 | 1207.66 | NaN | 1199.91 | 1229.20 | 1196.93 |
| 1 | 11/25/2022 | 1220.68 | NaN | 1175.37 | 1224.84 | 1196.93 |
| 2 | 11/24/2022 | 1174.08 | NaN | 1203.38 | 1204.51 | 1173.97 |
| 3 | 11/23/2022 | 1203.24 | NaN | 1163.50 | 1208.66 | 1180.91 |
| 4 | 11/22/2022 | 1163.99 | NaN | 1101.84 | 1169.88 | 1128.53 |
| ... | ... | ... | ... | ... | ... | ... |
| 382 | 11/05/2021 | 4486.79 | NaN | 4506.13 | 4507.00 | 4456.23 |
| 383 | 11/04/2021 | 4540.75 | NaN | 4562.61 | 4554.23 | 4470.10 |
| 384 | 11/03/2021 | 4552.30 | NaN | 4599.10 | 4604.84 | 4541.84 |
| 385 | 11/02/2021 | 4575.86 | NaN | 4306.76 | 4639.60 | 4556.01 |
| 386 | 11/01/2021 | 4314.85 | NaN | 4237.23 | 4346.39 | 4287.36 |
387 rows × 6 columns
# Convertimos la columna Date, de tipo string, a tipo "datetime"
ETH["Date"] = pd.to_datetime(ETH["Date"])
# Ordenamos las fechas de más antigua a más reciente
ETH = ETH.sort_values(by='Date', ascending=True)
# Información principal
fig = go.Figure(data=go.Ohlc(x=ETH['Date'],
open=ETH['Open'],
high=ETH['High'],
low=ETH['Low'],
close=ETH['Close/Last']))
# Añadir anotaciones adicionales
fig.update_layout(
title='Evolución precio Ethereum (NOV 2021 - NOV 2022)',
yaxis_title='Precio en dólares ($)',
shapes = [dict(
x0='2021-11-08', x1='2021-11-08', y0=0, y1=1, xref='x', yref='paper',
line_width=2)],
annotations=[dict(
x='2021-11-08', y=0.05, xref='x', yref='paper',
showarrow=False, xanchor='left', text='Ethereum All Time High (ATH) - 08/11/2021')]
)
# Mostrar gráfico
fig.show()
SAN = pd.read_csv('../05 - Estudio de técnicas de visualización de datos (PEC2)/SANTANDER.csv')
SAN
| Date | Close/Last | Volume | Open | High | Low | |
|---|---|---|---|---|---|---|
| 0 | 11/25/2022 | $2.92 | 2198617 | $2.88 | $2.93 | $2.88 |
| 1 | 11/23/2022 | $2.85 | 2298938 | $2.84 | $2.8675 | $2.83 |
| 2 | 11/22/2022 | $2.85 | 5516099 | $2.8 | $2.85 | $2.79 |
| 3 | 11/21/2022 | $2.75 | 4780279 | $2.69 | $2.76 | $2.68 |
| 4 | 11/18/2022 | $2.68 | 2017884 | $2.68 | $2.7 | $2.66 |
| ... | ... | ... | ... | ... | ... | ... |
| 2514 | 11/30/2012 | $7.3408 | 4081765 | $7.3312 | $7.4079 | $7.3025 |
| 2515 | 11/29/2012 | $7.3025 | 3788375 | $7.3121 | $7.3312 | $7.0704 |
| 2516 | 11/28/2012 | $7.2162 | 5926391 | $7.0246 | $7.2354 | $6.9767 |
| 2517 | 11/27/2012 | $7.0917 | 2863475 | $7.1108 | $7.1683 | $7.0629 |
| 2518 | 11/26/2012 | $7.1396 | 1671935 | $7.13 | $7.1683 | $7.1108 |
2519 rows × 6 columns
Hay que eliminar el símbolo del dólar ($) del dataframe, de lo contrario, en el gráfico OHLC, aunque se representarán correctamente las barras de forma gráfica, los valores de apertura y cierre serán nulos:
# Se eliminarán todos los símbolos del dólar del dataframe
SAN = SAN.replace(r'\$', '', regex=True)
SAN
| Date | Close/Last | Volume | Open | High | Low | |
|---|---|---|---|---|---|---|
| 0 | 11/25/2022 | 2.92 | 2198617 | 2.88 | 2.93 | 2.88 |
| 1 | 11/23/2022 | 2.85 | 2298938 | 2.84 | 2.8675 | 2.83 |
| 2 | 11/22/2022 | 2.85 | 5516099 | 2.8 | 2.85 | 2.79 |
| 3 | 11/21/2022 | 2.75 | 4780279 | 2.69 | 2.76 | 2.68 |
| 4 | 11/18/2022 | 2.68 | 2017884 | 2.68 | 2.7 | 2.66 |
| ... | ... | ... | ... | ... | ... | ... |
| 2514 | 11/30/2012 | 7.3408 | 4081765 | 7.3312 | 7.4079 | 7.3025 |
| 2515 | 11/29/2012 | 7.3025 | 3788375 | 7.3121 | 7.3312 | 7.0704 |
| 2516 | 11/28/2012 | 7.2162 | 5926391 | 7.0246 | 7.2354 | 6.9767 |
| 2517 | 11/27/2012 | 7.0917 | 2863475 | 7.1108 | 7.1683 | 7.0629 |
| 2518 | 11/26/2012 | 7.1396 | 1671935 | 7.13 | 7.1683 | 7.1108 |
2519 rows × 6 columns
# Convertimos la columna Date, de tipo string, a tipo "datetime"
SAN["Date"] = pd.to_datetime(SAN["Date"])
# Ordenamos las fechas de más antigua a más reciente
SAN = SAN.sort_values(by='Date', ascending=True)
fig = go.Figure(data=go.Ohlc(x=SAN['Date'],
open=SAN['Open'],
high=SAN['High'],
low=SAN['Low'],
close=SAN['Close/Last']))
# Añadir anotaciones adicionales
fig.update_layout(
title='Fluctuación precio acciones Banco Santander',
yaxis_title='Precio en dólares ($)',
shapes = [dict(
x0='2020-03-01', x1='2020-10-31', y0=0, y1=0.5, xref='x', yref='paper',
line_width=1)],
annotations=[dict(
x='2020-10-31', y=0.55, xref='x', yref='paper',
showarrow=False, xanchor='right', text='Pandemia COVID-19')]
)
fig.show()
AENA = pd.read_csv('../05 - Estudio de técnicas de visualización de datos (PEC2)/AENA.csv')
AENA
| Date | Close/Last | Volume | Open | High | Low | |
|---|---|---|---|---|---|---|
| 0 | 11/25/2022 | $131.5175 | 15.0 | $131.5175 | $131.5175 | $131.5175 |
| 1 | 11/23/2022 | $127.8075 | 1.0 | $127.8075 | $127.8075 | $127.8075 |
| 2 | 11/22/2022 | $126.2725 | 74.0 | $126.2725 | $126.2725 | $126.2725 |
| 3 | 11/21/2022 | $127.625 | NaN | $127.625 | $127.625 | $127.625 |
| 4 | 11/18/2022 | $127.625 | 65.0 | $127.625 | $127.625 | $127.625 |
| ... | ... | ... | ... | ... | ... | ... |
| 1902 | 05/08/2015 | $98.3 | NaN | $98.3 | $98.3 | $98.3 |
| 1903 | 05/07/2015 | $98.3 | NaN | $98.3 | $98.3 | $98.3 |
| 1904 | 05/06/2015 | $98.3 | NaN | $98.3 | $98.3 | $98.3 |
| 1905 | 05/05/2015 | $98.3 | NaN | $98.3 | $98.3 | $98.3 |
| 1906 | 05/04/2015 | $98.3 | 1300.0 | $98.3 | $98.3 | $98.3 |
1907 rows × 6 columns
# Se eliminarán todos los símbolos del dólar del dataframe
AENA = AENA.replace(r'\$', '', regex=True)
# Convertimos la columna Date, de tipo string, a tipo "datetime"
AENA["Date"] = pd.to_datetime(AENA["Date"])
# Ordenamos las fechas de más antigua a más reciente
AENA = AENA.sort_values(by='Date', ascending=True)
fig = go.Figure(data=go.Ohlc(x=AENA['Date'],
open=AENA['Open'],
high=AENA['High'],
low=AENA['Low'],
close=AENA['Close/Last'],
increasing_line_color= 'lime', decreasing_line_color= 'violet'))
fig.show()
Empresas del IBEX35: https://www.eleconomista.es/indice/IBEX-35/resumen
Descarga de datasets: https://www.nasdaq.com/market-activity/cryptocurrency/eth/historical
Creación gráficos OHLC (Documentación librería plotly): https://plotly.com/python/ohlc-charts/
Análisis de OHLC: https://www.investopedia.com/terms/o/ohlcchart.asp#:~:text=OHLC%20and%20candlestick%20charts%20show,close%20via%20a%20real%20body.
OHLC [VS.] Candlestick: https://financetrain.com/understanding-japanese-candlestick-charts-and-ohlc-charts